<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * news controller
 * @author		Jade Xia
 * @copyright	Copyright (c) 2010 - 2011 天夏网络.
 * @link		http://www.tianxianet.com
 */
class News extends Controller
{
	var $site_title;
	var $floatim = '';
	
	function News()
	{
		parent::Controller();
		$this->load->database();
		$this->load->model('com_model');
		$this->site_title=$this->config->item('sys_site_title');
		if($this->config->item('sys_is_open_im') == '1')
		{
			$this->floatim = $this->com_model->floatim();
		}
		if ($this->config->item('sys_cache_time') > 0) $this->output->cache($this->config->item('sys_cache_time'));
	}
	
	function detail()
	{
		$rd_id=$this->uri->segment($this->com_model->uri_segnum+1,0);
		if(!$rd_id) 
		{
			$tx_msg='<li>ID参数有误.</li>';
			echo_msg($tx_msg);
		}
		$query = $this->common_model->get_record('SELECT * FROM '.$this->db->dbprefix.'news WHERE is_trash = 0 AND id = ?',array($rd_id));
		if ( ! $query) echo_msg('<li>ID参数有误</li>');
		$nav_arr=array(
			'tb'=>'news_catalog',
			'base' =>CTL_FOLDER.'news/index',
			'nav_title'=>$query->title,
			'catalog_id' =>$query->catalog_id
		);
		$nav = $this->com_model->get_nav($nav_arr);
		$data = array(
			'site_title'		=> $nav['nav_title'].'--'.$this->site_title,
			'news'		=> $query,
			'page_keyword'		=> $query->keyword,
			'page_description'		=> $query->description,
			'nav'		=> $nav['nav_str'],
			'content_link' => $this->com_model->content_link($rd_id,$query->catalog_id,'news',CTL_FOLDER.'news/detail'),
			'nav_title'		=> $nav['nav_title'],
			'news_catalog'		=> $this->com_model->parse_ncatalog(0),
			'product_catalog'		=> $this->com_model->parse_pcatalog(0)
		);
		$this->db->simple_query('UPDATE '.$this->db->dbprefix.'news SET hits = hits + 1 WHERE id = '.$rd_id);
		$this->load->view(TPL_FOLDER.'news_detail',$data);
	}
	
	function index()
	{
		$catalog_id = $this->uri->segment($this->com_model->uri_segnum+1, 0);
		$offset = $this->uri->segment($this->com_model->uri_segnum+2, 0);
		$sql = 'SELECT id,title,title_color,summary,pic_path,hits,create_date FROM '.$this->db->dbprefix.'news WHERE is_trash = 0';
		if((int)$catalog_id > 0)
		{
			$sql .= " AND catalog_id like '%,{$catalog_id},%'";
		}
		$sql .= ' ORDER BY seqorder DESC,id DESC';
		$page=array(
			'page_base' => CTL_FOLDER.'news/index/'.$catalog_id,
			'offset'   => $offset,
			'per_page' => 15,
			'sql' => $sql
		);
		$query = $this->common_model->get_page_records($page);
		
		$nav_arr=array(
			'tb'=>'news_catalog',
			'base' =>CTL_FOLDER.'news/index',
			'nav_title'=>'新闻资讯',
			'catalog_id' =>$catalog_id
		);
		$nav = $this->com_model->get_nav($nav_arr);
		$data = array(
			'site_title'		=> $nav['nav_title'].'--'.$this->site_title,
			'news'		=> $query['query'],
			'paginate'		=> $query['paginate'],
			'nav'		=> $nav['nav_str'],
			'nav_title'		=> $nav['nav_title'],
			'news_catalog'		=> $this->com_model->parse_ncatalog(0),
			'product_catalog'		=> $this->com_model->parse_pcatalog(0)
		);
		$this->load->view(TPL_FOLDER.'news',$data);
	}
}
?>